package com.zhbcm.treebuilder.web;


import com.zhbcm.treebuilder.bean.SqArticle;
import com.zhbcm.treebuilder.common.Result;
import com.zhbcm.treebuilder.common.ResultUtil;
import com.zhbcm.treebuilder.service.ISqArticleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 朝花不迟暮
 * @since 2021-02-08
 */
@RestController
@RequestMapping("article")
@Api(tags = "文章功能接口")
public class SqArticleController
{
    @Autowired
    private ISqArticleService articleService;

    @PostMapping("/")
    @ApiOperation("添加或修改文章")
    public Result saveSqArticle(@RequestBody SqArticle article)
    {
        try
        {
            articleService.saveOrUpdate(article);
            return ResultUtil.success(article);
        } catch (Exception e)
        {
            e.printStackTrace();
        }
        return ResultUtil.error();
    }

    @DeleteMapping("/{id}")
    @ApiOperation("删除文章")
    public Result deleteArticleById(@PathVariable Long id)
    {
        try
        {
            articleService.removeById(id);
            return ResultUtil.success();
        } catch (Exception e)
        {
            e.printStackTrace();
        }
        return ResultUtil.error();
    }

    @GetMapping("/")
    @ApiOperation("查询文章带评论")
    public Result getArticleAndComment()
    {
        try
        {
            List<?> list = articleService.getArticleAndComment();
            return ResultUtil.success(list);
        } catch (Exception e)
        {
            e.printStackTrace();
        }
        return ResultUtil.error();
    }
}
